/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.impl;

import org.telosys.common.TelosysRuntimeException;


/**
 * 
 * 
 * @author Laurent GUERIN
 *  
 */
public class QueryAncestor
{
//    //--- SQL request to SELECT the elements of the list
//    private String     _SqlSelect    = "";
//
//    //--- SQL request to delete the elements of the list
//    private String     _SqlDelete    = "";
//
//    //--- SQL request to count the elements of the list
//    private String     _SqlCount     = "";

    //--- Query parameters
    private Object[]   _params            = null;

    private boolean[]  _paramsInitialized = null;

    //-----------------------------------------------------------------------------
    // CONSTRUCTOR(S)
    //-----------------------------------------------------------------------------
    protected QueryAncestor(int iParamCount) 
    {
        if ( iParamCount < 0 )
        {            
            _params = new Object[1];    
            _paramsInitialized = new boolean[1];
        }
        else
        {
            _params = new Object[iParamCount+1];            
            _paramsInitialized = new boolean[iParamCount+1];
        }
        //--- Init all the array (even the first ZERO index element)
        for (int i=0 ; i < _params.length ; i++ )
        {
            _params[i] = null ;
            _paramsInitialized[i] = false ;
        }
    	
    }
    
    protected void resetAllParameters(Object[] newParameters)
    {
    	if ( null == newParameters )
    	{
            _params = new Object[1];    
            _paramsInitialized = new boolean[1];
    	}
    	else
    	{
            _params = new Object[newParameters.length + 1];            
            _paramsInitialized = new boolean[newParameters.length + 1];
            int i = 1 ;
            for ( Object o : newParameters )
            {
                _params[i] = o ;
                _paramsInitialized[i] = true ;
                i++;
            }
    	}
    }
    
    //-----------------------------------------------------------------------------
    // INTERFACE IMPLEMENTATION
    //-----------------------------------------------------------------------------
    /* (non-Javadoc)
     * @see org.objectweb.telosys.dal.dao.ListQuery#getParamCount()
     */
    public int getParamCount()
    {
        if ( _params != null )
        {
            return _params.length - 1;            
        }
        else
        {
            return 0 ;
        }
    }
    
//    //-----------------------------------------------------------------------------
//    /* (non-Javadoc)
//     * @see org.objectweb.telosys.dal.dao.ListQuery#getParametersArray()
//     */
//    public Object[] getParametersArray()
//    {
//        Object[] paramsCopy = (Object[]) _params.clone();
//        return paramsCopy;
//    }
    
    public boolean allParametersInitialized()
    {
        for ( int i = 1 ; i < _params.length ; i++ )
        {
        	if ( _paramsInitialized[i] != true ) return false ;
        }
        return true ;
    }
    
    public Object[] getAllParameters()
    {
    	Object[] arrayOfParameters = new Object[_params.length - 1 ];
        for ( int i = 1 ; i < _params.length ; i++ )
        {
        	//--- Base 1 to Base 0 
        	arrayOfParameters[i-1] = _params[i] ;
        }
        return arrayOfParameters ;
    }
    //-----------------------------------------------------------------------------
    /* (non-Javadoc)
     * @see org.objectweb.telosys.dal.dao.ListQuery#getParametersString()
     */
    public String getParametersString()
    {
        StringBuffer sb = new StringBuffer(128);
        sb.append("[");
        for ( int i = 1 ; i < _params.length ; i++ )
        {
            Object obj = _params[i];
            if (i > 1)
            {
                sb.append("|");
            }
            if ( obj == null )
            {                
                sb.append("(null)");                
            }
            else
            {
                sb.append(obj.toString());                
            }
        }
        sb.append("]");
        return sb.toString();
    }

    //-----------------------------------------------------------------------------
    /* (non-Javadoc)
     * @see org.objectweb.telosys.dal.dao.ListQuery#setParameter(int, java.lang.Object)
     */
    public void setParameter(int index, Object oValue)
    {
        if ( index > 0 && index < _params.length )
        {
            _params[index] = oValue ;
            _paramsInitialized[index] = true ;
        }
        else
        {
            throw new TelosysRuntimeException("Query : setParameter : invalid index " + index + " ( 1 to " + (_params.length-1) + " expected )" );
        }
    }
    
    //-----------------------------------------------------------------------------
    /* (non-Javadoc)
     * @see org.objectweb.telosys.persistence.Query#setParameter(int, byte)
     */
    public void setParameter(int iIndex, byte value)
    {
    	setParameter(iIndex, new Byte(value) );
    }
    
    /* (non-Javadoc)
     * @see org.objectweb.telosys.persistence.Query#setParameter(int, short)
     */
    public void setParameter(int iIndex, short value)
    {
    	setParameter(iIndex, new Short(value) );
    }
    
    /* (non-Javadoc)
     * @see org.objectweb.telosys.dal.dao.ListQuery#setParameter(int, int)
     */
    public void setParameter(int iIndex, int value)
    {
    	setParameter(iIndex, new Integer(value) );
    }
    
    /* (non-Javadoc)
     * @see org.objectweb.telosys.dal.dao.ListQuery#setParameter(int, long)
     */
    public void setParameter(int iIndex, long value)
    {
    	setParameter(iIndex, new Long(value) );
    }

    //-----------------------------------------------------------------------------
    public void setParameter(int index, float value)
    {
    	setParameter(index, new Float(value) );
    }
    
    public void setParameter(int index, double value)
    {
    	setParameter(index, new Double(value) );
    }
    
    //-----------------------------------------------------------------------------
    /* (non-Javadoc)
     * @see org.objectweb.telosys.dal.dao.ListQuery#getParameter(int)
     */
    public Object getParameter(int iIndex)
    {
        if ( iIndex > 0 && iIndex < _params.length )
        {
            return _params[iIndex];
        }
        else
        {
            throw new TelosysRuntimeException("Query : getParameter : invalid index " + iIndex + "( 1 to " + (_params.length-1) + " expected )" );
        }        
    }
}